package com.mxchip.livestar.ui.groupcall

import android.content.res.ColorStateList
import android.graphics.Color
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.mxchip.livestar.R
import com.mxchip.livestar.base.ContactPOJO
import com.mxchip.livestar.ui.ItemHolder
import kotlinx.android.synthetic.main.item_group_call.*
import kotlinx.android.synthetic.main.item_group_call.check_box
import kotlinx.android.synthetic.main.item_group_call.name
import kotlinx.android.synthetic.main.item_group_call.number

class GroupCallAdapter(private val onClickListener: ((View, ContactPOJO) -> Unit)? = null) : RecyclerView.Adapter<ItemHolder>() {

    private var mData: List<ContactPOJO> = emptyList()
    private val mCheckBoxMap: MutableMap<ContactPOJO, Boolean> = mutableMapOf()

    fun setData(list: List<ContactPOJO>) {
        mData = list
        mCheckBoxMap.keys.filterNot { checkedContact ->
            list.any {
                it.contactNumber == checkedContact.contactNumber
            }
        }.forEach {
            mCheckBoxMap.remove(it)
        }
    }

    fun getSelectedContacts(): List<ContactPOJO> {
        return mCheckBoxMap.filter { it.value }.keys.toList()
    }

    override fun getItemCount(): Int {
        return mData.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
        val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.item_group_call, parent, false)
        return ItemHolder(view)
    }

    override fun onViewRecycled(holder: ItemHolder) {
        super.onViewRecycled(holder)

        holder.check_box.tag = null
        holder.check_box.visibility = View.VISIBLE
        holder.check_box.isChecked = false
    }

    override fun onBindViewHolder(holder: ItemHolder, position: Int) {
        val bean = mData[position]

        //绘制联系人名称
        holder.name.text = bean.contactName
        holder.number.text =  bean.contactNumber

        holder.pys_layout.visibility = View.GONE
        holder.pys_split_line.visibility = View.GONE


        holder.check_box.tag = bean

        mCheckBoxMap[bean]?.let {
            Log.e("GroupCallAdapter", "check_box onBindViewHolder $it ")
            holder.check_box.isChecked = it
        }

        holder.check_box.buttonTintList = createColorStateList(ContextCompat.getColor(holder.containerView.context, R.color.color_white_60), Color.GRAY, Color.GRAY, Color.GRAY)

        holder.check_box.setOnClickListener {

            val checkBox = it as CheckBox
            val contactPOJO = it.tag as ContactPOJO?

            contactPOJO?.let { pojo ->
                mCheckBoxMap[pojo] = checkBox.isChecked
            }

            Log.e("GroupCallAdapter", "check_box setOnClickListener $mCheckBoxMap")
        }

        holder.containerView.setOnClickListener {

            holder.check_box.apply {
                if(visibility != View.GONE && visibility != View.INVISIBLE) {
                    isChecked = !isChecked
                    callOnClick()
                }
            }

            onClickListener?.invoke(it, bean)
        }
    }

    companion object {
        fun createColorStateList(normal: Int, pressed: Int, focused: Int, unable: Int): ColorStateList {
            val colors = intArrayOf(pressed, focused, normal, focused, unable, normal)
            val states = arrayOfNulls<IntArray>(6)
            states[0] = intArrayOf(android.R.attr.state_pressed, android.R.attr.state_enabled)
            states[1] = intArrayOf(android.R.attr.state_enabled, android.R.attr.state_focused)
            states[2] = intArrayOf(android.R.attr.state_enabled)
            states[3] = intArrayOf(android.R.attr.state_focused)
            states[4] = intArrayOf(android.R.attr.state_window_focused)
            states[5] = intArrayOf()
            return ColorStateList(states, colors)
        }
    }

}